using UnityEditor;
using UnityEngine;

public class Packager : UnityEditor.Editor
{
    // 当前打包平台
    public static readonly BuildTarget BuildTarget = EditorUserBuildSettings.activeBuildTarget;
    public static string CHECKSUM_FILE = "checksum.asset";
    public static string ASSET_BUNDLE_DIR = "AssetBundles";
    public static string PUBLISHING_DIR = "Publishing";

    public static bool DEVELOPMENT = false;
    public static bool USE_MONO = false;
    
    // 打包脚本实例
    private static PackagerBase BuildPackager
    {
        get
        {
            if (BuildTarget == BuildTarget.Android)
                return new PackagerAndroid();
            else if (BuildTarget == BuildTarget.iOS)
                return new PackagerIOS();
            else if (BuildTarget == BuildTarget.StandaloneWindows64)
                return new PackagerWindows();

            return null;
        }
    }
    
    // 构建组
    public static BuildTargetGroup BuildGroup
    {
        get
        {
            if (BuildTarget == BuildTarget.Android)
                return BuildTargetGroup.Android;
            else if (BuildTarget == BuildTarget.iOS)
                return BuildTargetGroup.iOS;
            else if (BuildTarget == BuildTarget.StandaloneWindows64)
                return BuildTargetGroup.Standalone;
            else if (BuildTarget == BuildTarget.StandaloneOSX)
                return BuildTargetGroup.Standalone;

            return BuildTargetGroup.Android;
        }
    }
    
    [MenuItem("Tools/Build AssetBundle", false, 10)]
    private static void CreateAssetBundle()
    {
        UnityEngine.Debug.Log($"buildTarget: {BuildTarget.ToString()}");
        BuildPackager.BuildAssetBundle();
    }
    
    [MenuItem("Tools/Build Player/Android/Apk", false, 30)]
    private static void BuildPlayer_Android_Apk()
    {
        UnityEngine.Debug.Log("Build player.");
        BuildPackager.BuildPlayer();
    }
    
    [MenuItem("Tools/Build Player/Android/Gradle", false, 31)]
    private static void BuildPlayer_Android_Gradle()
    {
        UnityEngine.Debug.Log("Build player.");
        BuildPackager.BuildPlayer();
    }
    
    [MenuItem("Tools/Build Player/iOS", false, 32)]
    private static void BuildPlayer_iOS()
    {
        UnityEngine.Debug.Log("Build player.");
        BuildPackager.BuildPlayer();
    }
    
    [MenuItem("Tools/Build Player/Windows", false, 33)]
    private static void BuildPlayer_Windows()
    {
        UnityEngine.Debug.Log("Build player.");
        BuildPackager.BuildPlayer();
    }
    
    [MenuItem("Tools/Copy AssetBundle 2 StreamingAssets", false, 50)]
    private static void Copy2StreamingAssets()
    {
        BuildPackager.CopyAssetBundles2StreamingAssets();
    }
    
    [MenuItem("Tools/Copy AssetBundle 2 SomeWhere", false, 70)]
    private static void Copy2TargetDirectory()
    {
        string folderPath = EditorUtility.OpenFolderPanel("Select Folder", Application.dataPath, "");
        Debug.Log("=====  " + folderPath);
        BuildPackager.CopyBundle2TargetPath(folderPath);
    }
}
